/*******************************************************************************
 * Copyright (c) 2007 University of Oslo.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     University of Oslo - initial API and implementation
 *******************************************************************************/

package com.cea.papyrus.diagram.sequence.sedi.edit;


import org.eclipse.uml2.uml.NamedElement;

import com.cea.papyrus.core.editor.ModelManager;
import com.cea.papyrus.diagram.sequence.sedi.figures.ILabeledFigure;
import com.cea.papyrus.diagram.sequence.sedi.model.util.ModelUtil;
import com.cea.papyrus.diagraminterchange2.di2.GraphElement;

/**
 * Abstract edit part for NamedElements. The edit part installs a Direct Edit Policy that
 * allows the name of the NamedElement to be edited in-place. Subclasses may wish to
 * override {@link #performDirectEdit} as well as {@link #getCellEditorLocator()}. The
 * default Direct Edit Policy requires figures to implement
 * {@link com.cea.papyrus.diagram.sequence.sedi.figures.ILabeledFigure} in order to be editable.
 * 
 * @author frankd@ifi.uio.no
 */
public abstract class AbstractNamedEditPart<T extends NamedElement> extends
        AbstractDirectEditableEditPart<T> {

    public AbstractNamedEditPart(GraphElement model, ModelManager modelManager) {
        super(model, modelManager);
    }

    @SuppressWarnings("unchecked")
    protected void refreshVisualsLabel() {
        if (getFigure() instanceof ILabeledFigure) {
            GraphElement model = (GraphElement) getModel();
            ILabeledFigure figure = (ILabeledFigure) getFigure();
            figure.setLabel(ModelUtil.<NamedElement> getSemantic(model).getName());
        }
    }

    @SuppressWarnings("unchecked")
    public T getUmlElement() {
        GraphElement view = (GraphElement) getModel();
        return ModelUtil.<T> getSemantic(view);
    }
}
